package com.hry.store.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hry.store.cms.dao.ProductColorMapper;
import com.hry.store.cms.dao.ProductVersionMapper;
import com.hry.store.cms.dao.ProductVoMapper;
import com.hry.store.cms.dao.SecondCategoryMapper;
import com.hry.store.cms.pojo.ProductColor;
import com.hry.store.cms.pojo.SecondCategory;
import com.hry.store.cms.service.ProductVoService;
import com.hry.store.cms.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductVoServiceImpl implements ProductVoService {
    @Autowired
    private ProductVoMapper productVoMapper;
    @Autowired
    private SecondCategoryMapper secondCategoryMapper;
    @Autowired
    private ProductColorMapper productColorMapper;
    @Autowired
    private ProductVersionMapper productVersionMapper;

    /**
     * 查询所有商品信息
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo getAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductVo> productVoList = productVoMapper.getAll();
        PageInfo pageInfo = new PageInfo(productVoList);
        return pageInfo;
    }

    /**
     * 根据条件查询
     *
     * @param pageNum
     * @param pageSize
     * @param status
     * @param beginTime
     * @param endTime
     * @param topCategoryId
     * @param keyWords
     * @return
     */
    @Override
    public PageInfo withSelected(Integer pageNum, Integer pageSize, Integer status, String beginTime, String endTime, Integer topCategoryId, String keyWords) {
        PageHelper.startPage(pageNum, pageSize);
        if (beginTime.equals("")) {
            beginTime = null;
            endTime = null;
        } else {
            beginTime = beginTime + " 00:00:00";
            endTime = endTime + " 00:00:00";
        }
        if (keyWords.equals("")) {
            keyWords = null;
        }
        List<ProductVo> productVoList = productVoMapper.withSelected(status, beginTime, endTime, topCategoryId, keyWords);
        PageInfo pageInfo = new PageInfo(productVoList);
        return pageInfo;
    }

    /**
     * 更新商品
     * @return
     */
    @Override
    @Transactional
    public int updateProduct(Integer secondCategoryId, Integer editProductQuantity,  String productDesc, Integer editLunBoStatus, Integer editHotStatus, Integer editSellStatus, String colorCode) {
        SecondCategory secondCategory = new SecondCategory();
        secondCategory.setSecondCategoryId(secondCategoryId);
        secondCategory.setIsHotProduct(editLunBoStatus);
        secondCategory.setIsStartProduct(editHotStatus);
        secondCategory.setProductDesc(productDesc);
        secondCategory.setStatus(editSellStatus);
        int result1= secondCategoryMapper.updateByPrimaryKeySelective(secondCategory);

        ProductColor color = new ProductColor();
        color.setColorName(colorCode);
        color.setSecondCategoryId(secondCategoryId);
        color.setQuantity(editProductQuantity);
        int result2= productColorMapper.updateBySecondCategoryIdSelective(color);

        if (result1 == result2) {
            return result1;
        }

        return 0;
    }

    /**
     * 删除产品
     * @param secondCategoryId
     * @return
     */
    @Override
    public boolean deleteByCategoryId(Integer secondCategoryId) {
        int result1 = secondCategoryMapper.deleteByPrimaryKey(secondCategoryId);
        int result2 = productVersionMapper.deleteBySecondCategoryId(secondCategoryId);
        int result3 = productColorMapper.deleteBySecondCategoryId(secondCategoryId);
        if (result1 > 0 && result2 > 0 && result3 > 0) {
            return true;
        }
        return false;
    }
}
